package week12.chat;

import javax.swing.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Server
 * @description: 服务器
 * @date 2021/11/24 9:11
 */
public class Server {
    public static void main(String[] args) {
        ArrayList<MySocket> socketList = new ArrayList();

        try {
            ServerSocket ss = new ServerSocket(7777);
            System.out.println("服务器准备就绪");

            while (true) {
                Socket clientSocket = ss.accept();

                InputStream is = clientSocket.getInputStream();
                DataInputStream dis = new DataInputStream(is);

                String username = dis.readUTF();
                System.out.println("欢迎< " + username + " >加入群聊");
                MySocket mySocket = new MySocket(username, clientSocket, true);
                socketList.add(mySocket);

                new ServerThread(socketList, clientSocket).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class ServerThread extends Thread {
    ArrayList<MySocket> socketList;
    Socket socket;
    int index;

    public ServerThread(ArrayList<MySocket> socketList, Socket socket) {
        this.socketList = socketList;
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            String username = "";

            for (int i = 0; i < socketList.size(); i++) {
                if (socketList.get(i).getSocket() == socket) {
                    index = i;
                    username = socketList.get(i).getUsername();
                }
            }

            while (socketList.get(index).getOnline()) {
                String text = "";
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                text = dis.readUTF();

                for (int i = 0; i < socketList.size(); i++) {
                    if ((socketList.get(i).getSocket() != socket) && (socketList.get(i).getOnline())) {
                        OutputStream os = socketList.get(i).getSocket().getOutputStream();
                        DataOutputStream dos = new DataOutputStream(os);
                        dos.writeUTF(username + "：" + text);
                    }
                }
            }
        } catch (IOException e) {
            socketList.get(index).setOnline(false);
            e.printStackTrace();
        }
    }
}

class MySocket {
    private String username;
    private Socket socket;
    private Boolean isOnline;

    public MySocket() {
    }

    public MySocket(String username, Socket socket, Boolean isOnline) {
        this.username = username;
        this.socket = socket;
        this.isOnline = isOnline;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public Boolean getOnline() {
        return isOnline;
    }

    public void setOnline(Boolean online) {
        isOnline = online;
    }

    @Override
    public String toString() {
        return "MySocket{" +
                "username='" + username + '\'' +
                ", socket=" + socket +
                ", isOnline=" + isOnline +
                '}';
    }
}
